using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace PerfAssist.MemoryProfilerAdvanced.Editor.Treemap
{
    public class Group : IComparable<Group>, ITreemapRenderable
    {
        public List<Item> _items;
        public string _name;
        public Rect _position;
        private float _totalMemorySize = -1;

        public float totalMemorySize
        {
            get
            {
                if (_totalMemorySize != -1)
                    return _totalMemorySize;

                long result = 0;
                foreach (var item in _items) result += item.memorySize;
                _totalMemorySize = result;
                return result;
            }
        }

        public float[] memorySizes
        {
            get
            {
                var result = new float[_items.Count];
                for (var i = 0; i < _items.Count; i++) result[i] = _items[i].memorySize;
                return result;
            }
        }

        public Color color
        {
            get { return Utility.GetColorForName(_name); }
        }

        public int CompareTo(Group other)
        {
            return (int) (other.totalMemorySize - totalMemorySize);
        }

        public Color GetColor()
        {
            return color;
        }

        public Rect GetPosition()
        {
            return _position;
        }

        public string GetLabel()
        {
            var row1 = _name;
            var row2 = EditorUtility.FormatBytes((long) totalMemorySize);
            return row1 + "\n" + row2;
        }
    }
}